﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GRPCClass
{
    public class Worker
    {
        private TaskRequestCollection _tasklist;

        public Worker()
        {
            _tasklist = new TaskRequestCollection();
        }
        public string ID
        {
            get;
            set;
        }
        /// <summary>
        /// 地址
        /// </summary>
        public string Host
        {
            get;
            set;
        }
        /// <summary>
        /// 端口号
        /// </summary>
        public int Port
        {
            get;
            set;
        }
        /// <summary>
        /// 最大任务数
        /// </summary>
        public int MaxTaskCount;

        /// <summary>
        /// 任务队列
        /// </summary>
        public TaskRequestCollection TaskList
        {
            get
            {
                return _tasklist;
            } 
        }
        /// <summary>
        /// 是否可用
        /// </summary>
        public bool Enable
        {
            get;
            set;
        }
        /// <summary>
        /// 最后的反馈时间
        /// </summary>
        public DateTime LastEcho
        {
            get;
            set;
        }
        public ITaskSendNode AppTaskNode
        {
            get;
            set;
        }

        /// <summary>
        /// 执行任务,分配任务
        /// </summary>
        /// <param name="vtaskRequest"></param>
        /// <returns></returns>
        public TaskReply RunTask(TaskRequest vtaskRequest)
        {
            string msg;
            TaskReply tr = new TaskReply();
            tr.ID = vtaskRequest.ID;
            tr.TaskInfo = vtaskRequest;
            if (AppTaskNode != null)
            {
                AppTaskNode.AppWorker = this;
                msg = AppTaskNode.SendToNode(vtaskRequest);
                tr.Message = msg;
                if (!string.IsNullOrEmpty(msg))
                {
                    tr.Status = 405; 
                }
                else
                {
                    TaskList.Add(vtaskRequest);
                    tr.Status = 4;
                }
            }
            else
            {
                tr.Message = "没有设置AppTaskNode对象";
                tr.Status = 404;
            }
            return tr;
        }
    }
}
